package com.gitee.kamismile.stone.rpc.core.start;


import com.gitee.kamismile.stone.registry.api.IServiceDiscovery;
import com.gitee.kamismile.stone.registry.eureka.EurekaServiceDiscovery;
import com.gitee.kamismile.stone.rpc.api.IProtocol;
import com.gitee.kamismile.stone.rpc.api.engine.StoneAppEngine;
import com.gitee.kamismile.stone.rpc.common.properties.StoneRpcConfigProperties;
import com.gitee.kamismile.stone.rpc.common.utils.StoneBeanUtilsApplication;
import com.gitee.kamismile.stone.rpc.core.provider.producer.assembly.StoneRpcReferenceProcessor;
import com.gitee.kamismile.stone.rpc.core.proxy.IStoneProxy;
import com.gitee.kamismile.stone.rpc.core.proxy.imp.bytebuddy.BytebuddyProxy;
import com.gitee.kamismile.stone.rpc.core.proxy.imp.jdk.JDKProxy;
import com.gitee.kamismile.stone.rpc.grpc.GrpcProtocol;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

@Configuration
@EnableConfigurationProperties(StoneRpcConfigProperties.class)
@ComponentScan(basePackages = "com.gitee.kamismile.stone.rpc")
public class StoneRpcSpringConfiguration {


    //方法名作为bean名称
    @Bean
    StoneBeanUtilsApplication stoneBeanUtilsApplication() {
        return new StoneBeanUtilsApplication();
    }


    @Bean
    StoneRpcReferenceProcessor stoneRpcReferenceProcessor(StoneRpcConfigProperties stoneRpcConfigProperties,
                                                          StoneAppEngine stoneAppEngine, ApplicationContext applicationContext) {
        return new StoneRpcReferenceProcessor(stoneRpcConfigProperties, stoneAppEngine, applicationContext);
    }

    @Bean
    StoneAppEngine stoneAppEngine(StoneRpcConfigProperties stoneRpcConfigProperties,IProtocol iProtocol) {
        return new StoneAppEngine(stoneRpcConfigProperties,iProtocol);
    }

    @Bean
    @ConditionalOnMissingBean(IProtocol.class)
    @DependsOn("stoneBeanUtilsApplication")
    IProtocol protocol() {
        return new GrpcProtocol();
    }


    @Bean
    @ConditionalOnMissingBean(IServiceDiscovery.class)
//    @DependsOn("eurekaClient")
    IServiceDiscovery serviceDiscovery() {
        return new EurekaServiceDiscovery();
    }



    @Bean
    @ConditionalOnMissingBean(IStoneProxy.class)
    @DependsOn("stoneBeanUtilsApplication")
    IStoneProxy stoneProxy() {
        return new BytebuddyProxy();
    }
}
